<?php

namespace app\common\response;

use PhpOffice\PhpSpreadsheet\Writer\Exception;
use PhpOffice\PhpSpreadsheet\Writer\IWriter;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use think\Response;

class XlsxDownload extends Response
{
    protected IWriter $writer;
    private string|int $filename;

    public function __construct(Xlsx $writer,string|int $filename)
    {
        $this->writer = $writer;
        $this->filename = $filename;
    }

    /**
     * @throws Exception
     */
    protected function output($data)
    {
        header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8;");
        header("Content-Disposition: inline;filename=\"{$this->filename}.xlsx\"");
        header('Cache-Control: max-age=0');
        $this->writer->save('php://output');
    }

}
